var myApp = sy.getModule('myApp',[]);
var stepW = 38;
var description = new Array("非常差，回去再练练","真的是差，都不忍心说你了","一般，还过得去吧","很好，是我想要的东西","太完美了，此物只得天上有，人间哪得几回闻!");

myApp.directive('onFinishRenderFilters', function ($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    scope.$emit('ngRepeatFinished');
                });
            }
        }
    };
});

myApp.controller('MyController', function($scope,$http) {
  $scope.fid = "";
  $scope.order = {};
  $scope.order.id = _id;
  $scope.imgs = [];
  $scope.stars = 5;
  $scope.content = '';
  sy.openLoad();
  $http.post(sy.path + "/m/order/detail", {id:$scope.order.id}).success(function(data) {
	  	sy.closeLoad();
	  	if(data == "sessionOut")location.href = sy.path+"/m/login";
	  	if(data.code == 0){
	  		if(data.order.state != 4){
	  			alert("该订单无法评价");
	  			history.go(-1);
	  		}else{
	  			$scope.order = data.order;
	  		}
	  	}else{
	  		sy.alert(data.msg);
	  	}
	}).error(function(data, status) {
		sy.closeLoad();
		sy.alert('数据请求失败');
	});
  
  $scope.uploadImg = function(){
	  $("#img").trigger("click");
  }
  
  $scope.delImg = function(index){
	  if($scope.imgs[index]){
		  $scope.imgs.splice(index,1);
	  }
  }
  
  $scope.updateGoodsImgs = function(){
	  $scope.imgs.push(id);
	  $scope.$apply();
	  $("#img").val("");
	  sy.closeLoad();
  }
  
  $scope.submitComment = function(){
	  
	  sy.openLoad();
	  $http.post(sy.path + "/m/order/addComment", {orderId:$scope.order.id,stars:$scope.stars,content:$scope.content,imgs:$scope.imgs.join(",")}).success(function(data) {
		  	sy.closeLoad();
		  	if(data == "sessionOut")location.href = sy.path+"/m/login";
		  	if(data.code == 0){
		  		history.go(-1);
		  	}else{
		  		sy.alert(data.msg);
		  	}
		}).error(function(data, status) {
			sy.closeLoad();
			sy.alert('数据请求失败');
		});
  }
  
  $("#star li").click(function () {
	    var index = $("#star li").index(this);
		index=index+1;
		$("#star li a").removeClass("xx");
	     $("#star li:lt("+index+")").find("a").addClass("xx");
	     $scope.stars = index;
	 });
	 $("#star li").hover(function () {
	    var index = $("#star li").index(this);
		index=index+1;
		$("#star li a").removeClass("xx");
	     $("#star li:lt("+index+")").find("a").addClass("xx")
	 });
  
  $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
	  	
	});

});

function stopDefault(e){
    if(e && e.preventDefault)
           e.preventDefault();
    else
           window.event.returnValue = false;
    return false;
};

//回调
function callback(id){
	var appElement = document.querySelector('[ng-controller=MyController]');
	var $scope = angular.element(appElement).scope(); 
	$scope.updateGoodsImgs(id);
}

$(function(){
	$("#img").bind("change",function(){
		if($(this).val() == '')return;
		sy.openLoad();
		$("#form1").submit();
	});
})